package cn.devices.controller;


import cn.devices.entity.TbAuthDm;
import cn.devices.model.JsonMessage;
import cn.devices.service.TbAuthDmService;
import cn.hutool.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yaml.snakeyaml.constructor.DuplicateKeyException;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 部门权限表 前端控制器
 * </p>
 *
 * @author 秋水浮萍
 * @since 2021-02-07
 */
@RestController
@RequestMapping("/tb-auth-dm")
public class TbAuthDmController {


    @Autowired
    private TbAuthDmService tbAuthDmService;

    /**
     * 设备授权添加
     *
     * @param tbAuthDm
     * @return
     */
    @PostMapping("tbAuthDmAdd")
    public JsonMessage tbAuthDmAdd(@RequestBody TbAuthDm tbAuthDm) {
        JsonMessage message = new JsonMessage();
        try {
            tbAuthDm.setAuthorizeTime(new Date());
            tbAuthDmService.tbAuthDmAdd(tbAuthDm);
            message.setData("授权成功");
        } catch (DuplicateKeyException e) {
            message.setData("已存在");
        }
        return message;
    }

    /**
     * 设备授权删除
     *
     * @param typeId
     * @param deCode
     * @return
     */
    @GetMapping("tbAuthDmDelete")
    public JsonMessage tbAuthDmDelete(String typeId, String deCode) {
        JsonMessage message = new JsonMessage();
        tbAuthDmService.tbAuthDmDelete(typeId, deCode);
        message.setData("删除成功");
        return message;
    }

    /**
     * 根据部门号查询所有授权的类型
     *
     * @param deCode
     * @return
     */
    @GetMapping("tbAuthDmList")
    public JsonMessage tbAuthDmList(String deCode) {
        JsonMessage message = new JsonMessage();
        List<TbAuthDm> tbAuthDmList = tbAuthDmService.tbAuthDmList(deCode);
        message.setData(tbAuthDmList);
        return message;
    }


}
